package com.dsp.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.core.util.DspUtil;
import com.core.util.Pagination;
import com.core.util.PinYin;
import com.dsp.bean.InsureProgram;
import com.dsp.bean.Message;
import com.dsp.services.InsureProgramService;

@Controller("InsureProgramsController")
public class InsureProgramsController extends DspController<InsureProgram>{

	public InsureProgramsController() {
		super(InsureProgram.class);
	}

	Logger logger = Logger.getLogger(DspUtil.Log4J);

	@RequestMapping(value = "/insure_programs", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("insure_program/list");
		mav.addObject("path", "insure_program/list.jsp");
		mav.addObject("current", "insure_programs");
		return mav;
	}

	@RequestMapping(value = "/insure_programs_list")
	@ResponseBody
	public Pagination<InsureProgram> index(HttpServletRequest request, HttpServletResponse response) {
		super.initHttp(request, response);
		DetachedCriteria dc = DetachedCriteria.forClass(InsureProgram.class);
		Pagination<InsureProgram> p = this.insureProgramService.search(dc, (page - 1) * rp, rp);
		p.setPage(page);
		return p;
	}

	@RequestMapping(value = "/insure_programs_create", method = RequestMethod.POST)
	@ResponseBody
	public Message create(@RequestBody InsureProgram insureProgram) {
		Message message = new Message();
		logger.info("正在创建社保项目....");
		try {
			insureProgram.setCompany(-1);
			if (insureProgram.getId() != null) {
				this.insureProgramService.update(insureProgram);
			} else {
				insureProgram.setPyCode(new PinYin().toPinYin(insureProgram.getInsureName(), true, false));
				this.insureProgramService.save(insureProgram);
			}
		} catch (Exception se) {
			se.getStackTrace();
			message.setSuccess(false);
			List<String> msg = new ArrayList<String>();
			msg.add(se.getLocalizedMessage());
			message.setMsg(msg);
		}
		return message;
	}

	@RequestMapping(value = "/insure_programs_edit", method = RequestMethod.POST)
	@ResponseBody
	public InsureProgram edit(@RequestBody InsureProgram _insureProgram) {
		InsureProgram insureProgram = this.insureProgramService.findById(_insureProgram.getId());
		return insureProgram;
	}

	@RequestMapping(value = "/insure_programs_delete", method = RequestMethod.POST)
	@ResponseBody
	public Message delete(@RequestBody InsureProgram insureProgram) {
		Message message = new Message();
		String rtn = this.insureProgramService.delete(insureProgram);
		message.setSuccess(Boolean.valueOf(rtn));
		return message;
	}

	@Autowired
	private InsureProgramService insureProgramService;

	/**
	 * @return the insureProgramService
	 */
	public InsureProgramService getInsureProgramService() {
		return insureProgramService;
	}

	/**
	 * @param insureProgramService
	 *            the insureProgramService to set
	 */
	public void setInsureProgramService(InsureProgramService insureProgramService) {
		this.insureProgramService = insureProgramService;
	}
}
